package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.card.EntityXfRecordWaitpay;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.repairpay.GetRepairPayRecordListRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfRecordWaitpay;
import com.ocom.payconfig.service.XfRecordWaitpayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfRecordWaitpayServiceImpl extends ServiceImpl<IDaoXfRecordWaitpay, EntityXfRecordWaitpay> implements XfRecordWaitpayService {

    @Autowired
    private IDaoXfRecordWaitpay iDaoXfRecordWaitpay;

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public PageInfo<EntityXfRecordWaitpay> getXfRecordWaitpay(Long comId, boolean allComId, GetRepairPayRecordListRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());

        QueryWrapper<EntityXfRecordWaitpay> queryWrapper = Wrappers.query();

        if (!allComId) {
            queryWrapper.eq("com_id", comId);
        }
        if (0 == request.getStatus()) {
            queryWrapper.in("pay_state", -1, 2);
        } else if (1 == request.getStatus()) {
            queryWrapper.eq("pay_state", 1);
        } else if (3 == request.getStatus()) {
            queryWrapper.in("pay_state", -1,0,2);
        } else if (4 == request.getStatus()) {
            queryWrapper.eq("pay_state", request.getStatus());
        }
        if (null != request.getMerchId() && -1 != request.getMerchId()) {
            queryWrapper.eq("merch_id", request.getMerchId());
        }
        if (null != request.getPointId() && -1 != request.getPointId()) {
            queryWrapper.eq("point_id", request.getPointId());
        }
        if (StringUtils.isNotBlank(request.getDeviceNo())) {
            queryWrapper.eq("device_no", request.getDeviceNo());
        }
        if (request.getType()==1) {
            if (null != request.getStartTime()) {
                queryWrapper.ge("cons_time", request.getStartTime());
            }
            if (null != request.getEndTime()) {
                queryWrapper.le("cons_time", request.getEndTime());
            }
        }else{
            if (null != request.getStartTime()) {
                queryWrapper.ge("pay_time", request.getStartTime());
            }
            if (null != request.getEndTime()) {
                queryWrapper.le("pay_time", request.getEndTime());
            }
        }

        if (StringUtils.isNotBlank(request.getPName())) {
            queryWrapper.eq("p_name", request.getPName());
        }
        if (StringUtils.isNotBlank(request.getAccountNo())) {
            queryWrapper.eq("account_no", request.getAccountNo());
        }
        if (StringUtils.isNotBlank(request.getPCode())) {
            queryWrapper.eq("p_code", request.getPCode());
        }
        if (null != request.getPId()) {
            queryWrapper.eq("p_id", request.getPId());
        }
        if (request.getType()==1) {
            queryWrapper.orderByDesc("cons_time");
        }else{
            queryWrapper.orderByDesc("pay_time");
        }
        List<EntityXfRecordWaitpay> list = this.list(queryWrapper);
        return new PageInfo<>(list);
    }

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfRecordWaitpay> getRepairPaySum(Long comId, Long pId) {
        QueryWrapper<EntityXfRecordWaitpay> queryWrapper = Wrappers.query();
        queryWrapper.select("com_id," +
                " p_id," +
                " p_name," +
                " p_code," +
                " dept_name," +
                " DATEDIFF(now(),  min(create_time)) days," +
                " sum(0 - real_money) amount," +
                " sum(1) times");
        queryWrapper.eq("com_id", comId);
        queryWrapper.in("pay_state", -1,2);
        if (null != pId && 0 < pId) {
            queryWrapper.eq("p_id", pId);
        }
        queryWrapper.groupBy("p_id");
        queryWrapper.orderByDesc("p_id");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean removeById(Long comId, Long id) {
        return super.removeById(id);
    }

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean removeByOrderNo(Long comId, String orderNo) {
        QueryWrapper<EntityXfRecordWaitpay> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("order_no", orderNo);
        return super.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updatePayState(Long comId, EntityXfRecordWaitpay entity) {
        UpdateWrapper<EntityXfRecordWaitpay> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("order_no", entity.getOrderNo());
        if(entity.getPayState()==4){
            updateWrapper.eq("pay_state", -1);
        }
        updateWrapper.set("pay_state", entity.getPayState());
        updateWrapper.set("pay_time", entity.getPayTime());
        return super.update(updateWrapper);
    }
}
